<?php

namespace app\admin\controller;
use think\Controller;

class Login extends Controller {

    private $obj;
    public function _initialize()
    {
        $this->obj = model("Admin");
    }

    public function index(){
    	return $this->fetch();
    }

    public function check(){

        if(!request()->isPost()) {
            $this->error('请求失败');
        }
        $username = input('post.username');
        $password = input('post.password');

        if(!trim($username)) {
            return show(0, '用户名不能为空');
        }
        if(!trim($password)) {
            return show(0, '密码不能为空');
        }
        $ret = $this->obj->getAdminByUsername($username);
        if(!$ret) {
            return show(0, '该用户不存在');
        }

        if($ret['password'] != getMd5Password($password)) {
            return show(0, '密码错误');
        }
        session('adminUser', $ret, 'think');
        return show(1, '登录成功');
    }

    public function loginout(){
        session('adminUser', null, 'think');
        $this->redirect('/admin/login');
    }

}